<?php
$page_name=basename($_SERVER['SCRIPT_FILENAME']);
$IsPreserved	= 'Y';
$IsProcess		= $_REQUEST['IsProcess'];
include "general_include.php";
include "checklogin.php";
if($IsProcess <> 'Y')
{
	include "top.php";
	include "left.php";
}

$item_per_page=$_SESSION['item_per_page'] = $_REQUEST['item_per_page'];
	if(empty($item_per_page) || !isset($item_per_page))
	{
	$item_per_page=10;
	} 

$action	= $_REQUEST['action'];

if($action=="del")
{	
	if(!empty($delete_id))
	{
	
	  $DelSql = "DELETE FROM ".TABLEPREFIX."_user_fav_coach where fav_coach_id='$delete_id'";
	  $UserManagerObjAjax->Execute($DelSql);
	  
    }	  	 
}  

function findlanguage($language_id)
{
     if($language_id=='')
			return false;
			
		$SelectTypeSql = "SELECT language_name FROM ".TABLEPREFIX."_language WHERE language_id=$language_id";
		$SelectType = mysql_fetch_array(mysql_query($SelectTypeSql));
		return $SelectType[0];
}		
function findvalue($table,$fld_name,$fld_value,$find_value)
{
     if($fld_value=='')
			return false;
			
		$SelectTypeSql = "SELECT ".$find_value." FROM ".$table." WHERE $fld_name=".$fld_value;
		$SelectType = mysql_fetch_array(mysql_query($SelectTypeSql));
		return $SelectType[0];
}	


/* Front Pagination Object Creation Starts */		
	//$PaginationFrontObj=new PaginationClassFrontAjax(10,3,1);
/* Front Pagination Object Creation Ends */


$ReturnSortingArr=$SortingObjAjax->Sorting("ur.overall_rating ASC,u.date_registered DESC","ur.overall_rating",$OrderType);
$OrderBySql=$ReturnSortingArr['OrderBySql'];
$OrderLink=$ReturnSortingArr['OrderLink'];
$OrderType=$ReturnSortingArr['OrderType'];	

 $SelectcoachSQL="SELECT ufav.*,u.*,ur.* FROM ".TABLEPREFIX."_user_fav_coach ufav , ".TABLEPREFIX."_user u ,".TABLEPREFIX."_user_type_user_relation ur  WHERE 
 ur.user_type_id=1 and ur.user_id=u.user_id and u.is_active='Y' and u.user_id=ufav.coach_id and ufav.user_id='".$_SESSION['user_id']."'".$OrderBySql;
// $PreserveLink="from=$from&from_page=$from_page&".$OrderLink;
// $preserve_variable_object->PreserveVariableUpdate($PreserveLink);
 
 //$pagination_arr=$PaginationFrontObj->PaginationFrontAjax("PaginationFront",$SelectcoachSQL,$page_name."?");	
	/* pagination ends */
 $PaginationObjAjaxLatest=new PaginationClassAjax($item_per_page,"prev",'',"next","active",$adodbcon);
 $pagination_arr = $PaginationObjAjaxLatest->PaginationAjax($SelectcoachSQL,$page_name."?action=".$action."&item_per_page=".$item_per_page,"Managergeneral");	
 $CoachArr=$UserManagerObjAjax->GetRecords("All",$pagination_arr[0]);

 $NumCoach=count($CoachArr);
	
for($i=0;$i<$NumCoach;$i++)
{
	$CoachArr[$i]['name']=show_to_control($CoachArr[$i]['name']);
	$CoachArr[$i]['about']=show_to_control($CoachArr[$i]['user_about']);
	$Clanguage = explode(",",$CoachArr[$i]['language_ids']);
	$language='';
	for($l=0;$l<count($Clanguage);$l++)
	{
	   if($Clanguage[$l]<>'')
	    $language .=findlanguage($Clanguage[$l]).' , ';
	}
	$CoachArr[$i]['language']=substr($language,0,-2); 
	$CoachgameSql = "SELECT * FROM ".TABLEPREFIX."_user_game where user_id='".$CoachArr[$i]['user_id']."' and is_active='Y'";
	$CoachgameArr[$i] = $UserManagerObjAjax->GetRecords("All",$CoachgameSql);
    $Numcoachgame=count($CoachgameArr[$i]);
	for($g=0;$g<$Numcoachgame;$g++)
	{
	    $gameSql = "SELECT * FROM ".TABLEPREFIX."_game WHERE game_id ='".$CoachgameArr[$i][$g]['game_id']."'";
	    $gameRow =  $UserManagerObjAjax->GetRecords("Row",$gameSql);
	    
		$CoachgameArr[$i][$g]['game'] = $gameRow['game_name'];
		$CoachgameArr[$i][$g]['is_ladder'] = $gameRow['is_ladder'];
		$CoachgameArr[$i][$g]['is_race'] = $gameRow['is_race'];
		$CoachgameArr[$i][$g]['is_server'] = $gameRow['is_server'];
		$CoachgameArr[$i][$g]['is_rating'] = $gameRow['is_rating'];
		$CoachgameArr[$i][$g]['is_region'] = $gameRow['is_region'];
		
		$CoachgameArr[$i][$g]['ladder']= findvalue(TABLEPREFIX."_game_ladder","ladder_id",$CoachgameArr[$i][$g]['ladder_id'],"ladder_name");
		$Coachrace = explode(",",$CoachgameArr[$i][$g]['race_id']);
		$race='';
	    for($a=0;$a<count($Coachrace);$a++)
		{
		  if($Coachrace[$a]<>'')
			$race .= findvalue(TABLEPREFIX."_game_race","race_id",$Coachrace[$a],"race_title").' , ';
		} 
		$CoachgameArr[$i][$g]['race']= substr($race,0,-2);
		
    	$Coachserver = explode(",",$CoachgameArr[$i][$g]['server_id']);
		$server='';
		for($a=0;$a<count($Coachserver);$a++)
		{
		  if($Coachserver[$a]<>'')
			$server .= findvalue(TABLEPREFIX."_game_server","server_id",$Coachserver[$a],"server_name").' , ';
		} 
		$CoachgameArr[$i][$g]['server']= substr($server,0,-2);
		
		$Coachregion = explode(",",$CoachgameArr[$i][$g]['region_id']);
		$region='';
		for($a=0;$a<count($Coachregion);$a++)
		{
		  if($Coachregion[$a]<>'')
			$region .= findvalue(TABLEPREFIX."_game_region","region_id",$Coachserver[$a],"region_title").' , ';
		} 
		$CoachgameArr[$i][$g]['region']= substr($region,0,-2);
		
		$Coachrating = explode(",",$CoachgameArr[$i][$g]['rating_id']);
		$rating='';
		for($a=0;$a<count($Coachrating);$a++)
		{
		  if($Coachrating[$a]<>'')
			$rating .= findvalue(TABLEPREFIX."_game_rating","rating_id",$Coachrating[$a],"rating_title").' , ';
		} 
		$CoachgameArr[$a][$g]['rating']= substr($rating,0,-2);
		
		
		//$CoachgameArr[$i][$g]['game']= findvalue(TABLEPREFIX."_game","game_id",$CoachgameArr[$i][$g]['game_id'],"game_name");
		//$CoachgameArr[$i][$g]['ladder']= findvalue(TABLEPREFIX."_game_ladder","ladder_id",$CoachgameArr[$i][$g]['ladder_id'],"ladder_name");
		//$CoachgameArr[$i][$g]['race']= findvalue(TABLEPREFIX."_game_race","race_id",$CoachgameArr[$i][$g]['race_id'],"race_title");
		//$CoachgameArr[$i][$g]['server']= findvalue(TABLEPREFIX."_game_server","server_id",$CoachgameArr[$i][$g]['server_id'],"server_name");
		
	}  
	//$CoachArr[$i]['ladder']=findvalue(TABLEPREFIX."_game_ladder","ladder_id",$CoachArr[$i]['ladder_id'],"ladder_name");
	//$CoachArr[$i]['race']=findvalue(TABLEPREFIX."_game_race","race_id",$CoachArr[$i]['race_id'],"race_title");
	//$CoachArr[$i]['server']=findvalue(TABLEPREFIX."_game_server","server_id",$CoachArr[$i]['server_id'],"server_name");
	$totstar='';
	for($x=1;$x<=$CoachArr[$i]['overall_rating'];$x++)
	{
	  $totstar.="<img src='images/star-c.gif' alt='' border='0' />";
	}
	$istar=5 - $CoachArr[$i]['overall_rating']; 
	for($y=1;$y<=$istar;$y++)
	{
	  $totstar.="<img src='images/star-g.gif' alt='' border='0' />";
	}
	$CoachArr[$i]['star']=$totstar;
	$avail_type = explode(',',$CoachArr[$i]['availability_type']);
	if($avail_type[1]=='')
	{
	  if($avail_type[0]=='O')
	   $CoachArr[$i]['availability_type'] = 'Online'; 
	  if($avail_type[0]=='L')
	  {
	    $CoachArr[$i]['availability_type'] = 'Local meet-up'; 
		$CoachArr[$i]['avail_local'] = 'Y';
	  }	
	}
	else
	{
	   $CoachArr[$i]['availability_type'] = 'Online , Local meet-up';
	   $CoachArr[$i]['avail_local'] = 'Y';
	}   
	
	$CoachArr[$i]['availability_country'] = findvalue(TABLEPREFIX."_country","country_id",$CoachArr[$i]['availability_country'],"country_name");
}


$smarty->assign('page_name',$page_name);
$smarty->assign('from',$from);
$smarty->assign('from_page',$from_page);
$smarty->assign('PreserveLink',$PreserveLink);
$smarty->assign('OrderType',$OrderType);
$smarty->assign('CoachgameArr',$CoachgameArr);
$smarty->assign("CoachArr",$CoachArr);
$smarty->assign('pagination_arr',$pagination_arr);	
$smarty->assign("NumCoach",$NumCoach);
$smarty->display('my_coach.tpl');
if($IsProcess <> 'Y')
	 include "footer.php";
?>